#include"include.h"

void loggerInit()
{
   logger = malloc(sizeof(logger_t));
   int i;
   for(i=LOG_FILE;i<=LOG_OTHER;i++)
      logger->type[i] = 0;
   logger->level = LOG_NONE;
   logger->other = NULL;
   logger->file = NULL;
}

void loggerEnableFile( FILE *f)
{
   logger->type[LOG_FILE] = (f == NULL) ? 0 : 1;
   logger->file = f;
}

void loggerEnableStdout(short tf)
{
   logger->type[LOG_STDOUT] = tf;
}
void loggerEnableStderr( short tf)
{
   logger->type[LOG_STDERR] = tf;
}
void loggerEnableOther( void *other)
{
   logger->type[LOG_OTHER] = (other == NULL) ? 0 : 1;
   logger->other = other;
}

void loggerSetLevel( log_level lev)
{
   logger->level = lev;
}

void loggerLog(log_level level, char *message, ...)
{
   if(logger == NULL)
      loggerInit();
   va_list ap;

   if(level <= logger->level)
   {
      va_start(ap, message);
      if(logger->type[LOG_FILE])
      {
	 vfprintf(logger->file,message,ap);
      }
      if(logger->type[LOG_STDOUT])
      {
	 vfprintf(stdout,message,ap);
      }
      if(logger->type[LOG_STDERR])
	 vfprintf(stderr,message, ap);
      if(logger->type[LOG_OTHER])
	 logger->other(message, ap); 
      va_end(ap);
   }
}

void loggerExit()
{
   free(logger);
}
